<template>
	<view class="p-10">


		<view class="bg-white p-20 " style="padding-bottom: 50px">
			<view id="bm_top_height" class="p-b-10  a-c" style="flex-wrap: wrap;display: flex;">
				<view class="" style="margin: 10rpx;width: 150px;">
					<uni-data-select @change="change_category_id" :value="find.category_id"
						:localdata="localdata_category_list">
					</uni-data-select>
				</view>
				<view class="" style="margin: 10rpx;">
					<uni-easyinput confirm-type="search" @confirm="on_search" v-model="find.name" placeholder="请输入广告名称">
					</uni-easyinput>
				</view>
				<view class="banmi_flex">
					<view style="padding: 0 7rpx;"> <button @click="on_search()" type="primary" size="mini"> 查询
						</button>
					</view>
					<view style="padding: 0 7rpx;"> <button @click="on_search_resetting()" type="primary" size="mini">
							重置 </button>
					</view>
					<view style="padding: 0 7rpx;"> <button @click="on_add()" type="primary" size="mini"> 新增 </button>
					</view>

				</view>
			</view>
			<uni-table v-if="is_top_height" :top_height="top_height" border stripe @selection-change="selectionData">
				<uni-tr style=" position:sticky;top: 0;z-index: 999;">

					<uni-th align="center">id</uni-th>

					<uni-th align="center">广告类型</uni-th>
					<uni-th align="center">跳转类型</uni-th>

					<uni-th align="center">广告图片</uni-th>

					<uni-th align="center">广告标题</uni-th>

					<uni-th align="center">广告值</uni-th>

					<uni-th align="center">排序(越小越靠前)</uni-th>

					<uni-th align="center">创建时间</uni-th>

					<uni-th align="center">是否显示</uni-th>



					<uni-th align="center" class="operate_right">操作</uni-th>

				</uni-tr>
				<uni-tr v-for="(vo,index) in list" :key="index">
					<uni-td align="center">
						{{vo.id}}
					</uni-td>
					<uni-td align="center">
						{{getCategoryName(vo.category_id)}}
					</uni-td>
					<uni-td align="center">  
						{{getJumpName(vo.jump_id)}}
					</uni-td>
					<uni-td align="center">
						<image v-if="vo.img" class="b_y" @click="on_img(vo.img)" :src="vo.img|getImg"
							style="height: 120rpx;" mode="heightFix">
						</image>
					</uni-td>
					<uni-td align="center">
						{{vo.name}}
					</uni-td>
					<uni-td align="center">
						{{vo.value}}
					</uni-td>
					<uni-td align="center">
						{{vo.sort}}
					</uni-td>
					<uni-td align="center">
						{{vo.create_time|getTime}}
					</uni-td>
					<uni-td>
						<view @click="on_switch_open(index,vo.open)">
							<banmi-switch :is_state="vo.open" is_type='0'></banmi-switch>
						</view>
					</uni-td>


					<uni-td class="operate_right" buts="buts">
						<view class="t-c">
							<button @click="on_edit(index)" class="operate_button" type="primary" size="mini">
								编辑
							</button>
						</view>
						<view class="t-c">
							<button @click="on_dels(index)" class="operate_button button_del" type=""
								size="mini">删除</button>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
			<view class=" p-t-20 bg-white">
				<uni-pagination @change="getPage" show-icon="true" :total="total" current="1"
					style="float: left;margin-bottom: 10px;"></uni-pagination>
			</view>

			<edit ref="component_edit" @confirm="confirm_edit">
			</edit>
			<uni-popup ref="component_dels" type="center">
				<dels :id="id" @confirm="confirm_dels">
				</dels>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import edit from './edit.vue';
	import dels from './dels.vue';

	export default {
		components: {
			edit,
			dels
		},

		data() {
			return {

				localdata_category_list: [],
				find: {},
				list: [],
				total: 0,
				page: 1,
				id: 0,
				is_top_height: false,
			}
		},
		onLoad(on) {
			this.init_category_id();

		},
		created() {
			let that = this
			setTimeout(function() {
				let element = document.getElementById("bm_top_height");
				that.top_height = element.offsetHeight;
				that.is_top_height = true;
			}, 200);
		},
		methods: {
			getCategoryName(id) {
				for (var i = 0; i < this.localdata_category_list.length; i++) {
					if (this.localdata_category_list[i]['value'] == id) {
						var name = this.localdata_category_list[i]['text']
						return name;
					}
				}
				return '未找到分类';
			},
			getJumpName(id) {
				for (var i = 0; i < this.jump_list.length; i++) {
					if (this.jump_list[i]['value'] == id) {
						var name = this.jump_list[i]['text']
						return name;
					}
				}
				return '未找到跳转类型';
			},
			//全选 反选
			selectionData(list) {
				console.log("list: " + JSON.stringify(list));
			},
			on_dels(index) {
				this.id = this.list[index]['id'];
				this.$refs.component_dels.open()
			},
			confirm_dels() {
				this.init();
				this.$refs.component_dels.close()
			},
			on_img(img) {
				if (img.indexOf("http") != -1) {
					return img;
				}
				img = uni.getStorageSync('img_url') + img
				uni.previewImage({
					current: img,
					urls: [img]
				})
			},
			on_switch_open(index, val) {
				var that = this;
				var id = this.list[index]['id'];
				let requestParams = {
					id: id,
				};
				this.Http.post('/banmichaoqiangbizhi/admin/Ad/switch_open', requestParams).then(ret => {
					this.list[index]['open'] = ret.data
				})
			},
			on_edit(index) {
				this.id = this.list[index]['id'];
				this.$refs.component_edit.init(this.id)
			},
			confirm_edit() {
				if (this.id == 0) {
					this.page = 1;
				}
				this.init();

			},

			change_category_id(e) {
				this.find.category_id = e;
			},
			init_category_id() {
				var that = this;
				let requestParams = {};
				this.Http.post('banmichaoqiangbizhi/admin/Adcategory/lists', requestParams).then(ret => {
					if (ret.status == 200) {
						var old_list = ret.data.list;
						var list = [];
						for (var i = 0; i < old_list.length; i++) {
							var find = {
								text: old_list[i]['ct_name'],
								value: old_list[i]['id'],
							}
							list.push(find);
						}

						this.localdata_category_list = list;
						this.init_Adjump();
					}
				})

			},
			init_Adjump() {
				var that = this;
				let requestParams = {};
				this.Http.post('banmichaoqiangbizhi/admin/Adjump/lists', requestParams).then(ret => {
					if (ret.status == 200) {
						var old_list = ret.data.list;
						var list = [];
						for (var i = 0; i < old_list.length; i++) {
							var find = {
								text: old_list[i]['jump_name'],
								value: old_list[i]['id'],
							}
							list.push(find);
						}
		
						this.jump_list = list;
						this.init();
					}
				})
			
			},

			getPage(id) {
				this.page = id.current;
				this.init()
			},
			on_add() {
				this.id = 0;
				this.$refs.component_edit.init(0)
			},
			on_search() {
				this.init();
			},
			on_search_resetting() {
				this.find = {};
				this.page = 1;
				this.init();
			},
			init() {
				var that = this;
				this.find['page'] = this.page
				this.Http.post('banmichaoqiangbizhi/admin/Ad/lists', this.find).then(ret => {
					var list = ret.data.list.list.data;
					this.list = list;
					this.total = ret.data.list.list.total;
				})
			},
		}
	}
</script>

<style>
	.operate_button {
		width: 70px;
	}

	.operate_right {
		position: sticky;
		right: 0;
		z-index: 100;
		background-color: #fff;
	}
</style>